package com.cyanite.playlv2.widget;

import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.VenueCategory;
import com.cyanite.playlv2.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryPickerAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private int mLayoutToInflate;
    private Group<VenueCategory> mCategories;

    public CategoryPickerAdapter(Context context, Group<VenueCategory> categories) {
        super();
        mCategories = categories;
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.category_picker_list_item;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unnecessary
        // calls to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no
        // need to re-inflate it. We only inflate a new View when the
        // convertView supplied by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);

            // Creates a ViewHolder and store references to the two children
            // views we want to bind data to.
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.categoryPickerIcon);
            holder.name = (TextView) convertView.findViewById(R.id.categoryPickerName);
            holder.disclosure = (ImageView) convertView.findViewById(R.id.categoryPickerIconDisclosure);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        VenueCategory category = (VenueCategory) getItem(position);
        holder.icon.setImageResource(category.getIconVenueRes());
        holder.disclosure.setVisibility(View.GONE);
        holder.name.setText(category.getNameRes());

        return convertView;
    }


    private static class ViewHolder {
        ImageView icon;
        TextView name;
        ImageView disclosure;
    }

    @Override
    public int getCount() {
        return mCategories.size();
    }

    @Override
    public Object getItem(int position) {
        return mCategories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public static class CategoryFlat {
        private VenueCategory mCategory;
        private int mDepth;

        public CategoryFlat(VenueCategory category, int depth) {
            mCategory = category;
            mDepth = depth;
        }

        public VenueCategory getCategory() {
            return mCategory;
        }

        public int getDepth() {
            return mDepth;
        }
    }
}
